package com.example.lombokdemo.utils;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.example.lombokdemo.bo.HealthCertGenerateBo;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

/**
 * 健康咨询师证书生成工具类
 * @author 沈滨彬
 * 每次加一个字段，然后在上一个图的基础上再加一个字段块
 * 注意:在linux机器上需要安装好中文字体的
 */
@Slf4j
public class HealthCertGenerateUtil {

    private static final String sourceUrl = "https://pics0.baidu.com/feed/a8773912b31bb051de2ab41da720fbbe4aede059.jpeg?token=c5b1304eac8a61be93603e9092f483b1";
    private static final String sourceImgPath = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\指定总经.png";
    private static final String tempPath1 = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\2.png";
    private static final String tempPath2 = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\3.png";
    private static final String tempPath3 = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\4.png";
    private static final String tempPath4 = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\5.png";
    private static final String tempPath5 = "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\6.png";
    private static final String resultImgPath =  "E:\\文档\\仁弥科技\\盗铃空间\\盗龄记录\\证书\\healthCert.png";
    //    private static final String sourceImgPath = "/dlmall/mem/health/cert/daoling.png";
//    private static final String tempPath1 = "/dlmall/mem/health/cert/2.png";
//    private static final String tempPath2 = "/dlmall/mem/health/cert/3.png";
//    private static final String tempPath3 = "/dlmall/mem/health/cert/4.png";
//    private static final String tempPath4 = "/dlmall/mem/health/cert/5.png";
//    private static final String tempPath5 = "/dlmall/mem/health/cert/6.png";
//    private static final String resultImgPath = "/dlmall/mem/health/cert/healthCert.png";
    public static void main(String[] args) {
//        deletePics();

        HealthCertGenerateUtil.createHealthCert(HealthCertGenerateBo.builder()
                .certNo("232323")
                .userName("沈滨彬")
                .build());
        System.out.println("Hello World!");
    }

    public static String createHealthCert(HealthCertGenerateBo healthCertGenerateBo){
        try {
            String today = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
            String[] daySplitArray = today.split("-");
            FileUtil.writeFromStream(new URL(sourceUrl).openConnection().getInputStream(),sourceImgPath);
            drawTextInImg(sourceImgPath, healthCertGenerateBo.getUserName(), tempPath1, 57, 220, "black", 20.0f);
            drawTextInImg(tempPath1, healthCertGenerateBo.getCertNo(), tempPath2, 158, 312, "black", 10.0f);
            drawTextInImg(tempPath2, "仁弥网络科技有限公司", tempPath3, 150, 345, "black", 16.0f);
            drawTextInImg(tempPath3, daySplitArray[0], tempPath4, 151, 380, "black", 10.0f);
            drawTextInImg(tempPath4, daySplitArray[1], tempPath5, 191, 380, "black", 10.0f);
            drawTextInImg(tempPath5, daySplitArray[2], resultImgPath, 221, 380, "black", 10.0f);
        } catch (Exception e) {
            log.error("健康咨询师证书生成报错,入参:{},错误信息:{}", JSONUtil.toJsonStr(healthCertGenerateBo),e);
            e.printStackTrace();
        }
        return resultImgPath;
    }

    public static void drawTextInImg(String filePath, String text, String outPath,int left_n, int top_n,String color,float fsize) {
        ImageIcon imgIcon = new ImageIcon(filePath);
        Image img = imgIcon.getImage();
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bimage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bimage.createGraphics();

        //字体大小
        int fontSize = (int)fsize;
        int rectX = left_n;
        int rectY = top_n;
        int rectWidth = text.length() * (fontSize + 30);
        int rectHeight = fontSize + 8;

        //定义字体
        Font font = new Font("宋体",Font.BOLD, fontSize);
        g.setBackground(Color.white);
        g.drawImage(img, 0, 0, null);
        if("black".equals(color)){
            g.setPaint(Color.black);
        }else if("red".equals(color)){
            g.setPaint(Color.red);
        }
        g.setFont(font);
        //g.drawRect(left_n, top_n, rectWidth, rectHeight);
        g.drawString(text, rectX, top_n);
        g.dispose();

        try {
            FileOutputStream out = new FileOutputStream(outPath);
            ImageIO.write(bimage, "png", out);
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 删除图片
     */
    public static void deletePics() {
        try {
            FileUtil.del(sourceImgPath);
            FileUtil.del(tempPath1);
            FileUtil.del(tempPath2);
            FileUtil.del(tempPath3);
            FileUtil.del(tempPath4);
            FileUtil.del(tempPath5);
            FileUtil.del(resultImgPath);
        }catch (Exception e){
            log.error("图片删除失败:{}",e);
        }
    }
}